package com.example.springbootstudy.controller;


import com.example.springbootstudy.dao.DepartmentDao;
import com.example.springbootstudy.dao.EmployeeDao;
import com.example.springbootstudy.mapper.DepartmentMapper;
import com.example.springbootstudy.mapper.EmployeeMapper;
import com.example.springbootstudy.pojo.Department;
import com.example.springbootstudy.pojo.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Collection;
import java.util.List;

@Controller
public class EmployeeController {
    @Autowired
    private EmployeeDao employeeDao;
    @Autowired
    private DepartmentDao departmentDao;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private DepartmentMapper departmentMapper;





    @RequestMapping("/emps")
    public String list(Model model) {
        Collection<Employee> employees = employeeDao.getAllEmployees();
        model.addAttribute("emps",employees);
        return "emp/list";//返回到list页面
//
//        Collection<Employee> employees1 = employeeMapper.EmployeeList();
//        model.addAttribute("emps",employees1);
//        return "emp/list";
    }


    @GetMapping("/emp")
    public String add(Model model) {
        //查出所有的部门信息,添加到departments中,用于前端接收
        Collection<Department> departments = departmentDao.getDepartments();
        model.addAttribute("departments", departments);
        return "emp/add";//返回到添加员工页面
    }

    @PostMapping("/emp")
    public String addemp(Employee employee){
        employeeDao.addEmployee(employee);
        return "redirect:/emps";
    }

    //restful风格接收参数
    @RequestMapping("/edit/{id}")
    public String edit(@PathVariable("id") int id, Model model) {
        //查询指定id的员工,添加到empByID中,用于前端接收
        Employee employeeByID = employeeDao.getEmployeeByID(id);
        model.addAttribute("empByID", employeeByID);
        //查出所有的部门信息,添加到departments中,用于前端接收
        Collection<Department> departments = departmentDao.getDepartments();
        model.addAttribute("departments", departments);
        return "/emp/edit";//返回到编辑员工页面
    }

    @PostMapping("/edit")
    public String updateemp(Employee employee){
        employeeDao.addEmployee(employee);
        return "redirect:/emps";
    }

    @GetMapping("/delete/{id}")
    public String deleteemp(@PathVariable("id") int id){
        employeeDao.deleteEmployeeByID(id);

        return "redirect:/emps";
    }

    @RequestMapping("/signout")
    public String signout(HttpSession session){
        session.invalidate();
        return "index";
    }

    @GetMapping("/getdepartments")
    @ResponseBody
    public List<Department> getDepartments(){
        List<Department> departments = departmentMapper.getDepartments();
        return departments;
    }

    @GetMapping("/getemployee")
    @ResponseBody
    public List<Employee> queryEmployee(){
        List<Employee> employees = employeeMapper.EmployeeList();
        return employees;
    }




}

